package 滑动窗口;

import java.lang.management.ManagementFactory;

public class 长度最小的子数组1 {
    //https://leetcode.cn/problems/minimum-size-subarray-sum/description/
    public int minSubArrayLen1(int target, int[] nums) {
        int left = 0, right = 0, sum = nums[0];
        int len = Integer.MAX_VALUE;
        while(right < nums.length) {
            while(right < nums.length && sum < target) {
                right++;
                if(right < nums.length) {
                    sum += nums[right];
                }
            }
            if(right == nums.length) {
                break;
            }
            int tmp = right - left + 1;
            if(tmp < len) {
                len = tmp;
            }
            sum -= nums[left++];
            while(left < right && sum >= target) {
                sum -= nums[left];
                left++;
            }

            if(left == right) {
                if(nums[left] >= target) {
                    tmp = 1;
                    return 1;
                    //if(tmp < len) {
                    //len = tmp;
                    //}
                    //left++;
                    //right++
                    //continue;
                }
            }
            tmp = right - left + 1 + 1;
            if(tmp < len) {
                len = tmp;
            }
        }
        if(len == Integer.MAX_VALUE) {
            return 0;
        }
        return len;
    }
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length, sum = 0, len = Integer.MAX_VALUE;
        for(int left = 0, right = 0; right < n; right++) {
            sum += nums[right]; //进窗口
            while(sum >= target) { //判断
                len = Math.min(len, right - left + 1); //更新结果
                sum -= nums[left++];
            }
        }
        return len == Integer.MAX_VALUE ? 0 : len;
    }
    public static void main(String[] args) {
        long tmp =  (long)Integer.MAX_VALUE + 1;
    }
}
